package com.ctsi.ssdc.service.impl;

import com.ctsi.ssdc.entity.Excel14;
import com.ctsi.ssdc.entity.TradeAndServiceIndustry;
import com.ctsi.ssdc.mapper.Excel14Mapper;
import com.ctsi.ssdc.service.Excel14Service;
import com.ctsi.ssdc.service.ExcelService;
import com.ctsi.ssdc.utils.MathUtil;
import com.ctsi.ssdc.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 14基础表服务业分行业(Excel14)表服务实现类
 *
 * @author makejava
 * @since 2023-02-21 18:52:45
 */
@Service("excel14Service")
@Slf4j
public class Excel14ServiceImpl implements Excel14Service {
    @Resource
    private Excel14Mapper excel14Mapper;
    @Resource
    private ExcelService excelService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Excel14 queryById(Long id) {
        return this.excel14Mapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param excel14     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Excel14> queryByPage(Excel14 excel14, PageRequest pageRequest) {
        long total = this.excel14Mapper.count(excel14);
        return new PageImpl<>(this.excel14Mapper.queryAllByLimit(excel14, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param excel14 实例对象
     * @return 实例对象
     */
    @Override
    public Excel14 insert(Excel14 excel14) {
        this.excel14Mapper.insert(excel14);
        return excel14;
    }

    /**
     * 修改数据
     *
     * @param excel14 实例对象
     * @return 实例对象
     */
    @Override
    public Excel14 update(Excel14 excel14) {
        this.excel14Mapper.update(excel14);
        return this.queryById(excel14.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.excel14Mapper.deleteById(id) > 0;
    }

    /**
     * 批量增加
     *
     * @param list
     * @return
     */
    @Override
    public boolean insertBatch(List<Excel14> list) {
        return excel14Mapper.insertBatch(list) > 0;
    }


    /**
     * 交通及营利性服务业营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public TradeAndServiceIndustry getTrafficTurnoverTotal(String year, String month) {
        TradeAndServiceIndustry info = excel14Mapper.getTrafficTurnoverTotal(year, month);
        if (info != null) {
            info.setIndicators("交通及营利性服务业营业额");
            info.setStatisticalTime(TimeUtil.date(year, month));
        }
        return info;
    }

    /**
     * 根据日期删除
     *
     * @param date
     * @return 是否成功
     */
    @Override
    public boolean removeByDate(Date date) {
        return excel14Mapper.removeByDate(date);
    }

    /**
     * 批量修改值 (千元) -> (万元)
     * 需求 -> {
     * 22年里14页上服务业基础表单位是千元，改成亿元不要换算错了，其余基础表里都是万元。23年我都改成万元。
     * }
     *
     * @param year 年份
     * @return 成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeValue(String year) {
        log.info("changeValue -> year: {}" , year);
        List<Excel14> list = excel14Mapper.queryByYear(year);
        if (list == null) return false;
        list.forEach(e -> {
            if (e.getCurrentmonth() != null && e.getCurrentaccumulate() != null) {
                e.setCurrentmonth(MathUtil.round(e.getCurrentmonth() / 10, 2));
                e.setCurrentaccumulate(MathUtil.round(e.getCurrentaccumulate() / 10, 2));
                // 修改
                excel14Mapper.update(e);
            }
        });
        // 更新
        for (int i = 2; i <= 12; i++) {
            excelService.sheet14Cal(TimeUtil.date(year, String.valueOf(i)));
        }
        return true;
    }

    /**
     * 批量修改值 (千元) -> (万元)
     * 需求 -> {
     * 22年里14页上服务业基础表单位是千元，改成亿元不要换算错了，其余基础表里都是万元。23年我都改成万元。
     * }
     *
     * @param year 年份
     * @return 成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeValuePre(String year) {
        log.info("changeValue -> year: {}" , year);
        List<Excel14> list = excel14Mapper.queryByYear(year);
        if (list == null) return false;
        list.forEach(e -> {
            if (e.getPreviousmonth() != null && e.getPreviousaccumulate() != null) {
                e.setPreviousmonth(MathUtil.round(e.getPreviousmonth() / 10, 2));
                e.setPreviousaccumulate(MathUtil.round(e.getPreviousaccumulate() / 10, 2));
                // 修改
                excel14Mapper.update(e);
            }
        });
        // 更新
        for (int i = 2; i <= 12; i++) {
            excelService.sheet14Cal(TimeUtil.date(year, String.valueOf(i)));
        }
        return true;
    }


}
